﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace VPNNew.CustomControls
{
    [ToolboxData("<{0}:QuotaBar runat=server></{0}:QuotaBar>")]
    public class QuotaBar : WebControl
    {
        public int Total
        {
            get
            {
                object o = ViewState["Total"];
                return o == null ? 0 : (int)o;
            }
            set
            {
                ViewState["Total"] = value;
            }
        }

        public int Used
        {
            get
            {
                object o = ViewState["Used"];
                return o == null ? 0 : (int)o;
            }
            set
            {
                ViewState["Used"] = value;
            }
        }

        public int Remain
        {
            get
            {
                return Total - Used;
            }
        }


        protected override void Render(HtmlTextWriter writer)
        {
            double p;
            if (Total > 0)
            {
                p = ((double)Remain / (double)Total) * 100;
            }
            else
            {
                p = 0;
            }
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "qBar");
            writer.AddAttribute(HtmlTextWriterAttribute.Title, string.Format("[[Total]]: {0}MB, [[Used]]: {1}MB", Total, Used));
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "qBarF");

            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, string.Format("{0}%", p));
            writer.AddAttribute(HtmlTextWriterAttribute.Title, string.Format("[[Total]]: {0}MB, [[Remain]]: {1}MB", Total, Remain));
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderEndTag();

            writer.RenderEndTag();

            base.RenderContents(writer);
        }

    }
}
